<template>
  <div
    class="w-[350px] border border-gray-200 bg-white p-3 shadow-md rounded-lg"
    @click="openDrawer"
  >
    <Handle type="target" :position="Position.Left" />
    <div class="flex flex-col gap-y-4">
      <common-title-module />

      <CommonDisplayModule title="输入" :valueList="data?.inoutlist" />
    </div>
    <Handle type="source" :position="Position.Right" />
  </div>
  <InputDrawerModule
    ref="inputDrawerRef"
    :title="node?.node?.label"
    :form="data"
    @add="handleClickAddBtn"
    @remove="handleClickDeleteBtn"
    @cancel="onCancel"
  />
</template>
<script setup>
import { ref } from 'vue';
import { Handle, Position, useNode } from '@vue-flow/core';
import { editNode } from '@/api/modules/flow';
import InputDrawerModule from '../modules/input-drawer-module.vue';
import CommonTitleModule from '../modules/common-title-module.vue';
import CommonDisplayModule from '../modules/common-display-module.vue';

defineProps();
const node = useNode();

const data = ref({
  ...node.node.data,
  inoutlist: node.node.data?.inoutlist || [],
});


const inputDrawerRef = ref(null);
const openDrawer = () => {
  inputDrawerRef.value?.init();
};

function handleClickAddBtn() {
  data.value.inoutlist.push({
    varname: '',
    vartype: 'String',
    defaultval: '',
    vardesc: '',
    isrequired: false,
  });
  node.node.data = {
    ...node.node.data,
  };
}

function handleClickDeleteBtn(index) {
  data.value.valueList.splice(index, 1);
  node.node.data = {
    ...node.node.data,
  };
}

const onCancel = async () => {
  await editNode({
    data: { id: node.id, inoutlist: data.value.inoutlist },
  });
};
</script>
